Repository navigation
Evaluate substring operands once - #577
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
|
One tradeoff to flag before you review. To keep C++17 order (the arguments can modify the receiver, e.g. If you'd rather avoid the box, a plain local receiver could be read in place after the arguments are evaluated, and only receivers with side effects would keep the evaluate-once capture. That adds a second code shape for the rule, so I left it out. Happy to switch to it, or to match the The |
The field-receiver case kept a std::string by value in a struct, so the refcount expected output recorded libstdc++'s 32-byte layout and failed on libc++, where std::string is 24 bytes. Reach the string through a reference member instead: substr still takes its receiver from a struct field, and the emitted layout is only the pointer-sized field. Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
#577 made the refcount `substr` rule take its receiver by pointer, so `make().s.substr(7, 6)` now takes a pointer to a field of a temporary; it worked before #577. The converter emitted that pointer inside the block of `field_ref!` (likewise `field_ptr!` and `array_field_ptr!`), and Rust 2024 drops the temporaries of a block's tail expression at the end of the block. The temporary was dropped before `substr` read it, and the program panicked with `ub: dangling pointer`. Other pointers to a field of a temporary, such as one passed to a `const std::vector<int> &` parameter, failed the same way. The converter now binds a pointer to the temporary in a `match` around the outermost field macro, so the temporary lives until the end of the enclosing Rust statement or block. That covers uses within the same expression, such as `substr`: ```rust (match Rc::new(RefCell::new(({ make_0(calls.as_pointer()) }))).as_pointer() { __tmp_1 => field_ref!(__tmp_1, values).as_pointer(), }) ``` This also makes pointers to fields of aggregate temporaries, such as `Outer{...}.inner.values`, compile. Pointers to fields of lvalues and the unsafe model are unchanged. Still dangling in refcount, as before: - a `const` reference or range-for bound to a field of a temporary; - a field reached through an array element of a temporary, such as `make().items[1].values`, or through a reference that a call returns, such as `identity(make().s).substr(7, 6)`; - two such arguments of one call, such as `two(make().values, make().inner.values)`, which are bound to variables before the call; - constructor arguments, such as `H h(make().values);`, which are converted in a block of their own (the unsafe translation prints a wrong value there too). [Case and evidence](<https://app.perfloop.ai/t/oss/case_9f1n2zm9dz>) --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). To ask for a change, comment or review in plain words. [How Perfloop handles pull requests](https://perfloop.ai/docs/work/pull-requests). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
Make substring calls evaluate their receiver and arguments once in both output models. Duplicating an operand during inline substitution can change both the selected bytes and caller state; this handles the substring case of #575.
The side-effect cases in tests/unit/string.cpp compare substring bytes and final counters with C++ for position, count and receiver expressions.
The result remains an independent, null-terminated copy of the selected slice. No whole-source clone is introduced; reference-counted receivers must now be addressable. Existing range clipping and overflow handling are unchanged.
Testing
litontests/unit/string.cppandtests/unit/string_substr.cpp(Linux, Release build): pass. Both fail without this change.string_substr.cpphard-codes the libstdc++std::stringsize; a fix for that follows.clang-format --dry-run --Werroron the changed C++ files,rustfmt --checkon the changed rule files, andcargo clippy --all-targets --all-features -- -Dwarningsonrules/: clean.tests/unit/string_substr.cpp checks receiver-before-argument ordering, source mutations in either argument, independent result storage, and temporary, pointer, field and container-element receivers. The generated Rust expectations cover both models.
Related work: #576 changes vector proxy handling in the converter and binds repeated operands in vector rules; this change handles substring expansion.
Case and evidence
Generated by Perfloop. Human sponsor: Tomás Senart.